package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.ActionStateMsg;
import com.tim.app.server.entry.Topic;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.FollowResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.SearchActivity;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

import java.util.List;


/**
 * 话题
 */
public class TopicAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Topic> {

    private Context mContext;

    public TopicAdapter(Context mContext, List<Topic> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_topic, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, final int position, final Topic data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(),  holder.rivSubject);
        } else {
            BitmapLoader.ins().loadImage("", holder.rivSubject);
        }
        if (!TextUtils.isEmpty(data.getTopicName())) {
            holder.tvSubjectTitle.setText(data.getTopicName());
        }
        if (!TextUtils.isEmpty(data.getDigest())) {
            holder.tvSubjectSubTitle.setVisibility(View.VISIBLE);
            holder.tvSubjectSubTitle.setText(data.getDigest());
        } else {
            holder.tvSubjectSubTitle.setVisibility(View.GONE);
        }
        if (null != data.getActionStateMsg() && null != data.getActionStateMsg().getContentState()) {
            holder.tvFollow.setSelected(true);
            holder.tvFollow.setText(R.string.hasFollow);
        } else {
            holder.tvFollow.setSelected(false);
            holder.tvFollow.setText(R.string.unFollow);
        }
        if (position == getDataList().size() - 1) {
            holder.vLine.setVisibility(View.GONE);
        } else {
            holder.vLine.setVisibility(View.VISIBLE);
        }
        holder.tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!UserManager.ins().isLogin()){
                    ViewGT.gotoLoginActivity(mContext);
                    return;
                }
                followOrCancelFollow(holder.tvFollow, position, data);
            }
        });
    }

    /**
     * 关注/取消关注
     */
    private void followOrCancelFollow(final View view, final int position, final Topic data) {
        view.setEnabled(false);
        if (null != data.getActionStateMsg() && data.getActionStateMsg().getContentState() != null) {
            int id = data.getActionStateMsg().getContentState();
            API_User.ins().cancelFollow(SearchActivity.TAG, id, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_TOPIC, new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    view.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        getDataList().get(position).getActionStateMsg().setContentState(null);
                        notifyItemChanged(position);
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        } else {
            API_User.ins().addFollow(SearchActivity.TAG, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_TOPIC, new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    view.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        FollowResult result = new Gson().fromJson(resultValue, FollowResult.class);
                        if (null != result && null != result.getRes()) {
                            if ( getDataList().get(position).getActionStateMsg() == null) {
                                getDataList().get(position).setActionStateMsg(new ActionStateMsg());
                            }
                            getDataList().get(position).getActionStateMsg().setContentState(result.getRes().getConcern().getId());
                            notifyItemChanged(position);
                        }
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        }
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivSubject;
        TextView tvSubjectTitle, tvSubjectSubTitle;
        TextView tvFollow;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            rivSubject = (RoundedImageView) itemView.findViewById(R.id.rivSubject);
            tvSubjectTitle = (TextView) itemView.findViewById(R.id.tvSubjectTitle);
            tvSubjectSubTitle = (TextView) itemView.findViewById(R.id.tvSubjectSubTitle);
            tvFollow = (TextView) itemView.findViewById(R.id.tvFollow);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
